package com.cn.removewatermark.batchdownload;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/*
*
* */
public class Download {
    //线程核心数
    private final static int corePoolSize=5;
    //线程最大数
    private final static int  maximumPoolSize=10;
    //线程队列
    private  static final ArrayBlockingQueue<Runnable> arrayQueue=new ArrayBlockingQueue<>(12);
   //线程池
    private  static final ThreadPoolExecutor threadExecutor=new ThreadPoolExecutor(corePoolSize,maximumPoolSize,2, TimeUnit.SECONDS,arrayQueue);
    //下载完成标记
    private static volatile AtomicBoolean endFlag=new AtomicBoolean(new Boolean(false));


    //消息队列
    static ArrayBlockingQueue<String> messageQueue=new ArrayBlockingQueue<>(20);


    public static void setEndFlag(AtomicBoolean endFlag) {
        Download.endFlag = endFlag;
    }

    /*
    * 将url放到队列中，然后让线程进行处理
    * */
    public void putMessage(String url) throws InterruptedException {
        messageQueue.put(url);
        if (threadExecutor.getPoolSize()<maximumPoolSize){
            threadExecutor.execute(new Thread(new DownloadThead()));
        }

    }

}
